<?php
#
# Author: Cody Sorgenfrey
#

function googleAny()
{	
	printf("<script type='text/javascript'>
	var _gaq = _gaq || [];
	_gaq.push(['_setAccount', 'UA-34839077-1']);
	_gaq.push(['_trackPageview']);
	
	(function() {
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
	})();
	</script>");
}

function userIsTab()
{
	if ($_GET['tab']){
		if ($_GET['tab'] == "NO"){
			printf("<script type='text/javascript'>setWidthCookie(\"300\", \"%s\");</script>", $_SERVER['REQUEST_URI']);
		} else if ($_GET['tab'] == "YES"){
			printf("<script type='text/javascript'>setWidthCookie(\"700\", \"%s\");</script>", $_SERVER['REQUEST_URI']);
		}
	}
	if ($_REQUEST['userWidthCookie']){
		if ($_REQUEST['userWidthCookie'] >= "700"){
			return TRUE;
			exit;
		} else {
			return FALSE;
			exit;
		}
	} else {
		printf("<script type='text/javascript'>setWidthCookie(null, \"%s\");</script>", $_SERVER['REQUEST_URI']);
	}
}
if (userIsTab()) include_once("./tabMenu.php");
else include_once("./menu.php");
include_once("../mysql/common.php");
include_once("../mysql/database_info.php");

function Url()
{
	$pageURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
	if ($_SERVER["SERVER_PORT"] != "80"){
		$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
	} else {
		$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
	}
	return $pageURL;
}

function mprint_html_head($title, $meta_key, $meta_desc, $morehtml)
{
	$now = time();
	$YEAR = date('Y',$now);
	printf(" <!--\n");
	printf("#\n");
	printf("# (c)%s South Hill Calvary Chapel\n", $YEAR);
	printf("#\n");
	printf("-->\n");
	printf("<!DOCTYPE html>\n");
	printf("<html id='html' xmlns='http://www.w3.org/1999/xhtml'>\n\n");
	printf("<title>SHCC | %s</title>\n\n", $title);
	printf("<meta name='viewport' content='width=device-width, minimum-scale=1.0, maximum-scale=1.6'>\n");
	printf("<meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>\n");
	printf("<meta name='keywords' content='South, Hill, Calvary, Chapel, Puyallup, Washington, Church, %s'/>\n", $meta_key);
	printf("<meta name='description' content='%s'/>\n", $meta_desc);
	printf("<meta name='content-type' content='text/html'/>\n");
	printf("<meta name='apple-mobile-web-app-capable' content='yes' />\n");
	printf("<link rel='apple-touch-icon' href='images/WebClipIcon.png'>\n");
	printf("<meta name='apple-mobile-web-app-status-bar-style' content='black' />");
	if (userIsTab()){
		printf("<link rel='stylesheet' type='text/css' href='/css/tabMain.css'/>\n\n");
		printf("<link rel='stylesheet' type='text/css' href='/css/tabMenu.css'/>\n\n");
		printf("<link rel='stylesheet' type='text/css' href='/css/tabNeeds.css'/>\n\n");
		printf("<link rel='apple-touch-startup-image' href='/tabStartup.png'>\n");
	} else {
		printf("<link rel='stylesheet' type='text/css' href='/css/main.css'/>\n\n");
		printf("<link rel='stylesheet' type='text/css' href='/css/menu.css'/>\n\n");
		printf("<link rel='stylesheet' type='text/css' href='/css/needs.css'/>\n\n");
		printf("<link rel='apple-touch-startup-image' href='/phoneStartup.png'>\n");
	}
	printf("%s", $morehtml);
}

function mprint_java_head($morejava)
{
	printf("<script type='text/javascript' src='/js/modernizr.custom.69872.js'></script>\n");
	printf("<script type='text/javascript' src='http://www.southhillcalvary.org/js/JWMediaPlayer5.5/jwplayer.js'></script>\n");
	printf("<script type='text/javascript' src='http://www.southhillcalvary.org/js/JWMediaPlayer5.5/swfobject.js'></script>\n");
	printf("<script type='text/javascript' src='/js/jQuery.js'></script>\n");
	printf("<script type='text/javascript' src='/js/js.js'></script>\n");
	printf("<script type='text/javascript' src='/js/lendingLibrary.js'></script>\n");
	printf("<script type='text/javascript'>setWidthCookie();</script>\n");
	if (userIsTab()){
		printf("<script type='text/javascript' src='/js/tabMenuTrans.js'></script>\n");
		printf("<script type='text/javascript' src='/js/tabSlideGesture.js'></script>\n");
	} else {
		printf("<script type='text/javascript' src='/js/menuTrans.js'></script>\n");
		printf("<script type='text/javascript' src='/js/slideGesture.js'></script>\n");
	}
	printf("%s\n", $morejava);
	googleAny();
}

function print_logo()
{
	printf("<div id='logo'>\n");
	printf("\t<span onclick='link(\"/\")'><img id='logo' alt='South Hill Calvary Chapel' src='/images/shccMobileHeader.png' alt='SHCC :: Mobile'></img></span>\n");
	printf("</div>\n\n");
}

//creates footer.
function print_footer()
{
	$year = date("Y");
	printf("<div class='footer'>\n");
	printf("\tCopyright 2012-%d | South Hill Calvary Chapel - Puyallup, WA <br>All rights reserved.\n", $year);
	printf("\t<script>email('webservant','southhillcalvary.org','Mobile website support');</script><br>\n");
	printf("<a class='footer' href='http://www.southhillcalvary.org/php-mobile-redirection.php?mobile=NO&page=%s'>[Visit Full Site]</a>&emsp;\n", $_SERVER['REQUEST_URI']);
	if (count($_GET) == 0){
		$returnPage = $_SERVER['REQUEST_URI']."?tab=";
	} else if ($_GET['tab']){ 
		$returnPage = str_replace("tab=".$_GET['tab'], "", $_SERVER['REQUEST_URI'])."&tab=";
	} else {
		$returnPage = $_SERVER['REQUEST_URI']."&tab=";
	}
	if (userIsTab()){
		printf("<a class='footer' href='%s'>[Not on a Tablet?]</a><br><br>\n", $returnPage."NO");
	} else {
		printf("<a class='footer' href='%s'>[Visit Tablet Verson]</a><br><br>\n", $returnPage."YES");
	}
	printf("</div>\n\n");
}

//calls the above 3 functions all in one mamba-jamba function! And starts body.
function startPage ($pTitle, $pMeta_key, $pMeta_desc, $morehtml, $morejava)
{
	//redirects mobile users to main site if check box on mobile site is switched to "full site"
	if ($_REQUEST['mobile'] == 'NO') printf("<script type='text/javascript'>window.location= 'http://www.southhillcalvary.org/index.php?mobile=1';</script>");
	printf("<head>\n");
	mprint_html_head($pTitle, $pMeta_key, $pMeta_desc, $morehtml);
	mprint_java_head($morejava);
	printf("</head>\n\n");
	printf("<body id='content'>\n");
	printMainMenuAndStuff();
	printf("<div id='page'>\n");
	printf("<div id='fadeOut'>\n");
	printf("<div id='slideGrip'></div>\n");
	printShareSheet();
	print_menu_bar();
	print_logo();
	printf("<h2 style='margin-left:%s;'>%s</h2>\n","3%", $pTitle);
	printf("<div onclick='closeMenus()' class='pageContent'>\n");
}

function startPageNoPageContent ($pTitle, $pMeta_key, $pMeta_desc, $morehtml, $morejava)
{
	//redirects mobile users to main site if check box on mobile site is switched to "full site"
	if ($_REQUEST['mobile'] == 'NO') printf("<script type='text/javascript'>window.location= 'http://www.southhillcalvary.org/index.php?mobile=1';</script>");
	printf("<head>\n");
	mprint_html_head($pTitle, $pMeta_key, $pMeta_desc, $morehtml);
	mprint_java_head($morejava);
	printf("</head>\n\n");
	printf("<body id='content'>\n");
	printMainMenuAndStuff();
	printf("<div id='page'>\n");
	printf("<div id='fadeOut'>\n");
	printf("<div id='slideGrip'></div>\n");
	printShareSheet();
	print_menu_bar();
	print_logo();
}

//ends body and prints footer.
function endPage ()
{
	printf("</div>\n");
	print_footer();
	printf("</div>\n");
	printf("</div>\n");
	printf("</body>\n");
	printf("</html>");
}

function endPageNoContent ()
{
	printf("</div>\n");
	print_footer();
	printf("</div>\n");
	printf("</div>\n");
	printf("</body>\n");
	printf("</html>");
}

function msermon_table ($result, $num_rows,$table) 
{
	$header = array("Date","Title","Speaker");
	$width = array(80, 450, 170);
	table_header($header, $width);
	for ($i=0; $i<$num_rows; $i++) {
		$date=mysql_result($result,$i,"date");
		$php_time = strtotime($date);
		$xml_date = date("m/d/y", $php_time);
		$id=mysql_result($result,$i,"id");
		$book=mysql_result($result,$i,"book");
		$verse=mysql_result($result,$i,"verse");
		$name=mysql_result($result,$i,"name");
		$title=mysql_result($result,$i,"title");
		$row = array($xml_date, $title, add_title($name));
		mtable_row($row, mrow_class($i+1), $id);
	}
	table_footer(3);
}

function mrow_class ($x) 
{
   if (($x % 2) == 1) {
    	$row = 'row_odd';
    } else {
    	$row = 'row_even';
    }
   return $row;
}

function msermon_series_table ($result, $num_rows) 
{
	$header = array("Date","Series Title","Speaker");
	$width = array(80, 490, 270);
	table_header($header, $width);
	for ($i=0; $i<$num_rows; $i++) {
		$date=mysql_result($result,$i,"date");
		$php_date = strtotime($date);
		$date_output = date("m/d/y", $php_date);
		$title=mysql_result($result,$i,"title");
		$first=mysql_result($result,$i,"first");
		$last=mysql_result($result,$i,"last");
		$id=mysql_result($result,$i,"id");
		$table=mysql_result($result,$i,"table");
		$cell1="<span onclick='link(\"./sermon_series.php?id=".$id."&table=".$table."&title=".$title."\")'>".$date_output."</span>";
		$cell2="<span onclick='link(\"./sermon_series.php?id=".$id."&table=".$table."&title=".$title."\")'>".$title."</span>";
		$cell3="<span onclick='link(\"./sermon_series.php?id=".$id."&table=".$table."&title=".$title."\")'>".add_title($first." ".$last)."</span>";
		$row = array($cell1, $cell2,$cell3);
		table_row($row, row_class($i+1), NULL);
	}
	table_footer(sizeof($row));
}

function mevents_table ($result, $num_rows) 
{
	$now = time();
	$i=0;
	printf("<div id='eventsList'>\n");
	printf("<ul style='margin-left:-30px; list-style:none; font-family: Gnuolane;'>\n");
	while ($i < $num_rows) {
		$date=mysql_result($result,$i,"start");
		$no_start=mysql_result($result,$i,"no_start");
		$php_start_time = strtotime($date);
		$date=mysql_result($result,$i,"end");
		$no_end=mysql_result($result,$i,"no_end");
		$php_end_time = strtotime($date);
		$php_end_day = date("z", $php_end_time);
		$php_start_day = date("z",$php_start_time);
		if ($now > $php_end_time) {
			$i++;
			continue;
		}
		if ($no_start) {
			$duration = 0;
		} else if ($no_end) {
			$date1 = date("l, F jS, g:i a", $php_start_time);
			$duration = $date1;
		} else if ($php_end_day != $php_start_day) {
			$date1 = date("l, F jS, g:i a", $php_start_time);
			$date2 = date("l, F jS", $php_end_time);
			$duration = $date1 . " - " . $date2;
		} else {
			$date1 = date("l, F jS, g:i a", $php_start_time);
			$date2 = date("g:i a", $php_end_time);
			$duration = $date1 . " - " . $date2;
		}
		$duration = strtoupper($duration);
		$title=strtoupper(mysql_result($result,$i,"title"));
		$announcement=mysql_result($result,$i,"announcement");
		$ministry=mysql_result($result,$i,"ministry");
		$flyer=mysql_result($result,$i,"flyer");
		$directions=mysql_result($result,$i,"directions");
		$id=mysql_result($result,$i,"id");
		$picture=mysql_result($result,$i,"picture");
		printf("<li class='event'>\n");
		if ($no_start) {
			printf("\t\t<H4>$title<a name='$id'></a></H4>\n");
		} else {
			printf("\t\t<H4> $title - $duration<a name='$id'></a></H4>\n");
		}
		printf("\t\t<div class='paragraph'>\n");
		printf("\t\t\t<p style='margin-top:-25px;'>%s</p>\n", $announcement);
		if (!empty($picture)) {
			printf("\t\t\t<span onclick='link(\"/event/%s\")'>[MORE DETAILS]</span>\n", $id);
		} else if (!empty($flyer) || !empty($directions)) {
			if (!empty($flyer)) {
				printf("<span onclick='link(\"%s\")' target='_blank'> [SEE FLYER] </span>\n",$flyer);
			}
			if (!empty($directions)) {
				printf("<span onclick='link(\"%s\")' target='_blank'> [DIRECTIONS] </span>\n",$directions);
			}
		}
		printf("\t\t\t</div>\n");
		printf("\t\t</li>\n");
		$i++;
	}
	printf("</ul>\n");
	printf("</div>\n");
}

function mpicture_caption ($pic, $width, $height, $caption, $margin) 
{
	printf("<div>\n");
	if ($width > 0 && $height > 0) {
		printf("\t<img src='%s' style='width:%s; height:%s; margin:%s;'/>\n", $pic, $width, $height, $margin);
	} else if ($width > 0) {
		printf("\t<img src='%s' style='width:%s; margin:%s;' />\n", $pic, $width, $margin);
	} else if ($height > 0) {
		printf("\t<img src='%s' style='height:%s; margin:%s;' />\n", $pic, $height, $margin);
	} else {
		printf("\t<img src='%s'/>\n", $pic);
	}
	printf("\t<H4 class='caption'>\n");
	printf("\t\t%s\n", $caption);
	printf("\t</H4>\n");
	printf("</div>\n");
}

function mMinistry_event_table ($result, $num_rows) 
{	
	printf("<h4>Events</h4>\n");
	while ($event = mysql_fetch_array($result)) {
		$xml_date = date("m/d/y", strtotime($event['start']));
		printf("<div>\n");
		if (empty($picture)) {
			printf("\t<span onclick='link(\"/events.php#%s\")'>\n", $event['id']);
		} else {
			printf("\t<span onclick='link(\"/event/%s\")'>\n", $event['id']);
		}
		if ($no_start) {
			printf("- [%s]</span>\n", $event['title']);
		} else {
			printf("%s<br>- [%s]</span>\n", $xml_date, $event['title']);
		}
		printf("</div>\n");
	}
}

function mMinistry_resource_table ($resource, $title) 
{
	printf("<h4>%s</h4>\n", $title);
	$num_rows = sizeof($resource);
	printf("<ul style='padding-left:20px;'>\n");
	for ($i=0;$i<$num_rows;$i++) {
		printf("\t<li style='margin-left:0px;'>\n");
		if ($resource[$i][0] == "") {
			printf("\t\t%s\n", $resource[$i][1]);
		} else if (strpos($resource[$i][0], "ttp://") || strpos($resource[$i][0], ".pdf")) {
			printf("\t\t<span target='_blank' onclick='link(\"%s\")'>[%s]</span>\n", $resource[$i][0], $resource[$i][1]);
		} else {
			printf("\t\t<span onclick='link(\"%s\")'>[%s]</span>\n", $resource[$i][0], $resource[$i][1]);
		}
		printf("\t</li>\n");
	}
	printf("</ul>\n");
}

function Mcm_pc_table ($result, $num_rows, $current, $day) 
{
	if ($day == 'Sunday') {
	$classes = array("","Nursery","Walkers", "2&3 Year Olds", "4yrs-K","1st-5th Grade");
	$header = array("Class","Service","Month","Title");
	} else {
	$classes = array("","2-4 Year Olds", "4yrs-5th Grade");
	$header = array("Class","Month","Title");
	}
	$now = time();
	Mtable_header($header, NULL);
	$i=0;
	while ($i < $num_rows) {
		$date=mysql_result($result,$i,"date");
		$php_date = strtotime($date);
		$current_year = date("Y", $now);
		$year = date("Y", $php_date);
		if ($current && $current_year != $year) {
		  $i++;
		  continue;
		}
		$current_month = date("F", $now);
		$month = date("F", $php_date);
		if ($current && $current_month != $month) {
			$i++;
			continue;
		}
		$service=mysql_result($result,$i,"service");
		if ($service == 1) {$service = "1st";}
		else if ($service == 2) {$service = "2nd";}
		else {$service = "All services";}
		$class=mysql_result($result,$i,"class");
		$link=mysql_result($result,$i,"link");
		$title=mysql_result($result,$i,"title");
		$cell1=$classes[$class];
		$cell2=$service;
		$cell3=$month;
		if (strpos($link, "docs.google.com")) {
			$cell4="<a href='".$link."' target='_blank'>".$title."</a>";
		} else {
			$cell4="<span onclick='link(\"http://www.southhillcalvary.org".$link."\")' target='_blank'>".$title."</span>";
		}
		if ($day == 'Sunday') {
			$row = array($cell1, $cell2,$cell3, $cell4);
		} else {
			$row = array($cell1, $cell3, $cell4);
		}
		Mtable_row($row, row_class($i+1), NULL);
		$i++;
	}
	Mtable_footer(sizeof($row));
}

function Mtable_header ($header, $width) 
{
  $len = sizeof($header);
  printf("<TABLE style='width:%s;'>\n","100%");
  printf("\t<tr>\n");
  for ($i = 0; $i < $len; $i++) {
	  printf("\t\t<td width='%dpx'><h5>%s</h5></td>\n", $width[$i], $header[$i]);
	}
  printf("\t</tr>\n");
}

function Mtable_row ($row, $row_class, $id) 
{
	$len = sizeof($row);
	if (empty($id)) {
		printf("\t<tr onclick='link(\"/sermon/%s\")' class='dotted_lines %s'>\n", $id, $row_class);
	} else {
		printf("\t<tr onclick='link(\"/sermon/%s\")' class='dotted_lines %s' id='%s'>\n", $id, $row_class,  htmlentities($id));
	}
	for ($i = 0; $i < $len; $i++) {
		printf("\t\t<td style='padding:5px;'>%s</td>\n", $row[$i] );
	}
	printf("\t</tr>\n");
}

function MContactTableRow ($row, $row_class, $id, $table) 
{
	if ($table == "Church Leadership and Staff"){
		$table = "people";
	}
	else {
		$table = "contact_ministry";
	}
	$len = sizeof($row);
	if (empty($id)) {
		printf("\t<tr onclick='link(\"/contactDetail.php?id=%s&table=%s\")' class='dotted_lines %s'>\n", $id, $table, $row_class);
	} else {
		printf("\t<tr onclick='link(\"/contactDetail.php?id=%s&table=%s\")' class='dotted_lines %s' id='%s'>\n", $id, $table, $row_class,  htmlentities($id));
	}
	for ($i = 0; $i < $len; $i++) {
		printf("\t\t<td style='padding:5px; width:%s;'>%s</td>\n", "50%", $row[$i] );
	}
	printf("\t</tr>\n");
}

function Mtable_footer ($cols) 
{
	printf("\t<tr class='dotted_lines'><td colspan=%d>&nbsp</td></tr>\n", $cols);
	printf("</TABLE>\n");
}

function Mcm_wed_table ($result, $num_rows) 
{
	$classes = array("","4yrs-5th Grade","Walkers-3yrs");
	$header = array("Date","Class","Verse","Title");
	Mtable_header($header, NULL);
	$i=0;
	while ($i < $num_rows) {
		$date=mysql_result($result,$i,"date");
		$php_time = strtotime($date);
		$xml_date = date("F jS, Y", $php_time);
		$book=mysql_result($result,$i,"book");
		$verse=mysql_result($result,$i,"verse");
		$title=mysql_result($result,$i,"title");
		$link=mysql_result($result,$i,"link");
		$class=mysql_result($result,$i,"class");
		$cell2=$classes[$class];
		$cell3=$book." ".$verse;
		$cell4="<a href='".$link."' target='_blank'>".$title."</a>";
		$row = array($xml_date, $cell2,$cell3,$cell4);
		Mtable_row($row, row_class($i+1), NULL);
		$i++;
	}
	Mtable_footer(sizeof($row));
}

function mContact_table ($result, $num_rows, $title) 
{
	printf("<table style='width:%s;'>\n", "100%");
	printf("\t<tr>\n");
	printf("\t\t<td colspan='4'><h4>%s</h4></td>\n", $title);
	printf("\t</tr>\n");
	for ($i=0; $i < $num_rows; $i++) {
		$position=mysql_result($result,$i,"position");
		$first=mysql_result($result,$i,"first");
		$last=mysql_result($result,$i,"last");
		$id = mysql_result($result, $i, "id");
		$cell2=add_title($first." ".$last);
		$row = array($position, $cell2);
		MContactTableRow($row, row_class($i+1), $id, $title);
	}
	table_footer(4);
}

function mFindSchedules ($name) 
{
	$table = "schedules";
	connect_select_test($table);
	$query="SELECT * FROM $table WHERE ministry='$name' ORDER BY start_date DESC";
	$result=mysql_query($query) or die (mysql_error());
	$num_rows=mysql_num_rows($result);
	$now = time();
	for ($i = 0; $i < $num_rows; $i++) {
		$end_date = mysql_result($result,$i,"end_date");
		$php_end_date = strtotime($end_date);
		if ($php_end_date < $now) {
			break;
		}
		$start_date = mysql_result($result,$i,"start_date");
		$php_start_date = strtotime($start_date);
		$start_year = date("Y", $php_start_date);
		$start_month = date("F", $php_start_date);
		$php_end_date = $php_end_date - (24 * 60 * 60); //go back one day
		$end_year = date("Y", $php_end_date);
		$end_month = date("F", $php_end_date);
		$LINK = mysql_result($result,$i,"link");
		if (stristr($LINK, "/ministries")){
			$LINK = "http://www.southhillcalvary.org".$LINK;
		}
		if ($start_month == $end_month) {
			$DATE = $start_month." ".$start_year;
		} else if ($start_year != $end_year) {
			$DATE = $start_month." ".$start_year."-".$end_month." ".$end_year;
		} else {
			$DATE = $start_month."-".$end_month." ".$end_year;
		}
		printf("<span onclick='link(\"%s\")' target='_blank'>[%s]</span>, ", $LINK, $DATE);
	}
}

function ministryContactBlock($result)
{
	printf("<p>%s<br>\n", $result[0]);
	printf("%s<br>\n", $result[1]);
	printf("<a href='mailto:%s;'>[%s]</a></p>\n", $result[2], $result[2]);
}

function mPrintNeedsLogin($page, $vars)
{
	printf("<form style='width:%s; margin-left:%s;' action='%s'>\n", "50%", "28%", $page);
	printf("\t<input type='text' name='user' placeholder='User Name' value='%s' />\n", $_GET['user']);
	printf("\t<input type='password' name='pass' placeholder='Password' value='%s' />\n", $_GET['pass']);
	if (!empty($vars)){
		$numVars = count($vars);
		for ($i=0;$i<$numVars;$i=$i+2){
			printf("\t<input type='hidden' name='%s' value='%s' />\n", $vars[$i], $vars[$i+1]);
		}
	}
	printf("\t<input type='submit' value='Login' />\n");
	printf("</form>\n");
	if ($_GET['user'] && $_GET['pass']){
		if ($_GET['user'] == "shcc" && $_GET['pass'] == "Fam1ly"){
			printf("<script type='text/javascript'>document.cookie= \"needsLogin=1\"; window.location = \"%s\";</script>", $page);
		} else{
			printf("<font style='color:red;'>Incorrect User Name or Password.</font>\n");
		}
	}
}
?>